package cn.jimmie.lib.qlog.interceptor;

import android.util.Log;

import cn.jimmie.lib.qlog.Options;
import cn.jimmie.lib.qlog.QLogLevel;

/**
 * Func : Android中的Log日志作为默认的控制台日志
 *
 * <p> Create Time:  2019-12-18 10:52 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class AndroidLogInterceptor implements ConsoleInterceptor {
    private boolean enabled = true;
    private Options options;

    @Override
    public void init(HeaderInterceptor header) {
        this.options = header.options();
    }

    @Override
    public boolean enabled() {
        return enabled;
    }

    @Override
    public void enabled(boolean enable) {
        this.enabled = enable;
    }

    @Override
    public void submit(QLogLevel level, String message) {
        if (level.level < options.level().level) return;
        print(level, message);
    }

    @Override
    public void release() {
    }

    private void print(QLogLevel level, String message) {
        switch (level) {
            case VERBOSE:
                Log.v(options.tag(), message);
                break;
            case DEBUG:
                Log.d(options.tag(), message);
                break;
            case INFO:
                Log.i(options.tag(), message);
                break;
            case WARN:
                Log.w(options.tag(), message);
                break;
            case ERROR:
                Log.e(options.tag(), message);
        }
    }
}
